class Solution {
public:
    long long maximumSumOfHeights(vector<int>& a) {
        long long ma = 0;
        int n = a.size();
        for (int i = 0; i < n; i++) {
            long long ans = a[i];
            int x = a[i];
            for (int j = i - 1; j >= 0; j--) {
                x = min(x, a[j]);
                ans += x;
            }
            x = a[i];
            for (int j = i + 1; j < n; j++) {
                x = min(x, a[j]);
                ans += x;
            }
            ma = max(ma, ans);
        }
        return ma;
    }
};